#!/bin/bash 

# 如果脚本没收到任何参数，直接返回
if [ $# -eq 0 ]; then
    echo "Usage: $0 <single-digit> 代表所启动超融合节点的索引" >&2
    exit 1
fi

# 判断 $1 是否为“一位数字”
if [ -z "$1" ] || \
   { case "$1" in
         [0-9]) ;;          # 唯一字符落在 0-9 之间，通过
         *) exit 1;;        # 其它情况立即失败
     esac; } 2>/dev/null
then
    :  # 符合要求，继续往下走
else
    echo "Error: '$1' 不是一位数字" >&2
    exit 1
fi

# 主逻辑
echo "传入的一位数字是：$1"

#set -x

i=$1

create_connecting_to_bridge_script_for_qemu(){
# 创建多个连接到bridge的qemu网卡脚本

cat <<EOF > add_to_manage_bridge.sh
#!/bin/bash

if ! ip link show hci_manage; then
	
	brctl addbr hci_manage
	ip addr add 192.168.10.254/24 dev hci_manage
fi

brctl addif hci_manage \$1
ip link set \$1 up
ip link set hci_manage up

EOF

cat <<EOF > join_storage_bridge.sh
#!/bin/bash

if ! ip link show storage1; then
	brctl addbr storage1
fi

brctl addif storage1 \$1
ip link set \$1 up
ip link set storage1 up

EOF


cat <<EOF > join_vxlan_bridge.sh
#!/bin/bash

if ! ip link show vxlan1; then
	brctl addbr vxlan1
fi

brctl addif vxlan1 \$1
ip link set \$1 up
ip link set vxlan1 up

EOF


	chmod +x *.sh
}

source "../ovs_helper.sh"

connect_manage=$(create_switch hci_manage)
connect_vxlan=$(create_switch hci_vxlan)
connect_storage=$(create_switch hci_storage)
connect_business=$(create_switch hci_business)

args=(
	
  # 必须使用e1000 网卡，不然网络能ping通，但是VRM无法添加CNA
  # 并且第一个网口必须得是管理网口  
  -device e1000,netdev=net0,mac=aa:51:00:00:0$i:00 \
  -netdev tap,id=net0,ifname=hci${i}_manage,script="$connect_manage" \
 
  -device e1000,netdev=net1,mac=aa:51:00:00:0$i:01 \
  -netdev tap,id=net1,ifname=hci${i}_1,script="$connect_vxlan" \

  -device e1000,netdev=net2,mac=aa:51:00:00:0$i:02 \
  -netdev tap,id=net2,ifname=hci${i}_2,script="$connect_storage" \
  #-netdev socket,id=net2,connect=127.0.0.1:31002

  -device e1000,netdev=net3,mac=aa:51:00:00:0$i:03 \
  -netdev tap,id=net3,ifname=hci${i}_3,script="$connect_business" \
  #-netdev socket,id=net3,connect=127.0.0.1:31003

  -device e1000,netdev=net4,mac=aa:51:00:00:0$i:04 \
  -netdev tap,id=net4,ifname=hci${i}_4,script=no 
  #-netdev socket,id=net4,connect=127.0.0.1:31004

  -device e1000,netdev=n1 \
  -netdev user,id=n1,hostfwd=tcp::2000$i-:22,hostfwd=tcp::2100$i-:443 \


  # 物理内存不够的场景下，可以调大宿主机的swap分区内存	  
  -m 64G 
)

firmware_path="$PWD/firmware_sangfor_hci"


	disk_file="$firmware_path/hci$i.qcow2"

	if [ ! -f $disk_file ]; then
		echo 该超融合节点还没有专属系统盘，现为其创建链接克隆

		#qemu-img create -f qcow2 -b $firmware_path/hci_base.qcow2 -F qcow2 $disk_file

		qemu-img create -f qcow2  $disk_file  500G

	fi



for((index=0;index<6;index++)); do




	storage_disk="$firmware_path/disk/hci_${i}_storage_${index}.qcow2"

	if [ ! -f $storage_disk ]; then
		echo 该超融合节点还没有专属硬盘，现为其创建链接克隆

		#qemu-img create -f qcow2 -b $firmware_path/hci_base.qcow2 -F qcow2 $disk_file

		qemu-img create -f qcow2  $storage_disk  500G

	fi

	args+=(
		
		
  		-drive file=$storage_disk,if=virtio 
	)
done


qemu-system-x86_64 \
  -enable-kvm \
  -cpu host \
  -smp 16 \
  -machine q35,accel=kvm \
  -drive file=$disk_file,if=virtio \
  -vnc 0.0.0.0:3$i,password=off \
  -daemonize \
  -cdrom ./firmware_sangfor_hci/HCI6.10.0_R2_X86_20240911_.iso \
  -smbios "type=1,manufacturer=HP,product=ProLiant DL380 G7,serial=CNG12345,uuid=11111111-2222-3333-4444-555555555555" \
  ${args[@]}


